package xtu.www.inject;

import android.app.Activity;
import android.util.ArrayMap;
import android.view.View;

import xtu.www.inject.provide.ActivityProvide;
import xtu.www.inject.provide.Provider;
import xtu.www.inject.provide.ViewProvide;

/**
 * Created by huilin on 2017/6/10.
 */

public class ViewInject {
    private static final ActivityProvide activityProvide = new ActivityProvide();
    private static final ViewProvide viewProvide = new ViewProvide();
    private static final ArrayMap<String, Inject> injectArrayMap = new ArrayMap<>();

    public static void inject(Activity activity) {
        inject(activity, activity, activityProvide);
    }

    public static void inject(View view) {
        inject(view, view);
    }

    public static void inject(Object host, View view) {
        inject(host, view, viewProvide);
    }

    public static void inject(Object host, Object source, Provider provider) {
        String className = host.getClass().getName();
        Inject inject = injectArrayMap.get(className);
        if (inject == null) {
            try {
                Class<?> aClass = Class.forName(className + "$$ViewInject");
                inject = (Inject) aClass.newInstance();
                injectArrayMap.put(className, inject);
                inject.inject(host, source, provider);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
